<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="村庄名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="townName">
              <!-- <a-input v-model="model.townName" placeholder="请输入村庄名称"></a-input> -->
              <j-select-depart
                v-if="!model.id"
                v-model="model.townName"
                :multi="false"
                placeholder="请选择"
                @selectDepart="selectDepart"
              />
              <a-input v-else v-model="model.realName" disabled placeholder="请输入村庄名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终点管点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pointNo">
              <!-- <a-input v-model="model.pointNo" placeholder="请输入终点编号"></a-input> -->
              <a-select
                v-model="model.pointNo"
                show-search
                placeholder="请输入终点编号"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                :options="pointDatalist"
                @search="querySearchPipPointAsync"
                @change="handleSelectPipPoint"
              >
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终点管点经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="townLongitude">
              <a-input-number v-model="model.longitude" placeholder="管点经度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="终点管点纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="townLatitude">
              <a-input-number v-model="model.latitude" placeholder="管点纬度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="长度（米）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="length">
              <a-input-number v-model="model.length" placeholder="请输入长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pipeDiameter">
              <a-input-number v-model="model.pipeDiameter" placeholder="请输入管径" style="width: 100%" />
              <!-- <j-dict-select-tag v-model="model.pipeDiameter" placeholder="请选择" dictCode="ps_pip_diameter" /> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="area">
              <!-- <a-input v-model="model.area" placeholder="请输入区域"></a-input> -->
              <j-select-depart v-model="model.area" :multi="false" placeholder="请选择" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接入污水处理厂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="factory">
              <!-- <a-input v-model="model.factory" placeholder="展示市级范围内所有污水处理厂"></a-input> -->
              <a-select
                v-model="model.factory"
                placeholder="展示市级范围内所有污水处理厂"
                show-search
                :filter-option="filterDrainOption"
              >
                <a-select-option v-for="item in factoryList" :key="item.id" :value="item.facilityName">
                  {{ item.facilityName }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="计划时间（年）"
              :labelCol="rowLabelCol"
              :wrapperCol="rowWrapperCol"
              prop="planDate"
            >
              <a-input-number v-model="model.planDate" placeholder="请输入计划时间（年）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="纳厂状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nachangStatus">
              <j-dict-select-tag v-model="model.nachangStatus" placeholder="请选择" dictCode="ps_nachang_status" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="纳厂终端" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nachangTerminal">
              <j-dict-select-tag v-model="model.nachangTerminal" placeholder="请选择" dictCode="ps_zdnc_actuality" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="是否列入省级开工任务"
              :labelCol="{
                xs: { span: 24 },
                sm: { span: 6 },
              }"
              :wrapperCol="{
                xs: { span: 24 },
                sm: { span: 18 },
              }"
              prop="isIncluded"
            >
              <a-radio-group v-model="model.isIncluded">
                <a-radio :value="1">是 </a-radio>
                <a-radio :value="2">否 </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="rowLabelCol" :wrapperCol="rowWrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue, simpleDebounce } from '@/utils/util'

export default {
  name: 'PaishuiDyNachangForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        planDate: 2024,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      rowLabelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
      },
      rowWrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        townName: [
          {
            required: true,
            message: '请输入村庄名称!',
          },
        ],
        pointNo: [
          {
            required: true,
            message: '请输入终点编号!',
          },
        ],
      },
      url: {
        add: '/nachang/paishuiDyNachang/add',
        edit: '/nachang/paishuiDyNachang/edit',
        queryById: '/nachang/paishuiDyNachang/queryById',
      },
      factoryList: [],
      pointDatalist: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.querySearchPipPointAsync = simpleDebounce(this.querySearchPipPointAsync, 500)
  },
  mounted() {
    this.getFactoryList()
  },
  methods: {
    filterDrainOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    selectDepart(row) {
      console.log('村庄信息', row[0])
    },
    // 管点-远程模糊搜索
    async querySearchPipPointAsync(pointNo) {
      if (!pointNo) {
        return
      }
      const res = await getAction(`/paishui/pipPoint/list?pipNo=*${pointNo}*`)
      const pointDatalist = res.result.records || []
      this.pointDatalist = pointDatalist.map((item) => ({
        value: item.pipNo,
        label: item.pipName,
        longitude: item.longitude,
        latitude: item.latitude,
      }))
    },
    handleSelectPipPoint(pointNo) {
      // console.log('handleSelectPipPoint', pointNo)
      const pointItem = this.pointDatalist.find((item) => item.value === pointNo)
      this.model.longitude = pointItem.longitude
      this.model.latitude = pointItem.latitude
    },
    async getFactoryList() {
      const areaId = '1638530806733377538'
      const res = await getAction(`/paishui/paishuiFacility/list/${areaId}`, {
        pageNo: 1,
        pageSize: 999,
        facilityType: 1,
        column: 'createTime',
        order: 'desc',
      })
      if (res.success) {
        this.factoryList = res.result.records
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(this.model)
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
